// pages/index/setPatkTicket/setPatkTicket.js
let request = require('../../../utils/request.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    shopId: null,
    shopName: null,
    selectedParkList: [],
    parkList: [],
    index: 0,
    shopParkTicketList: [],
    selectedParkTicket: {},
    toBeActivatedValidityDays: null,
    quantity: null,
    activationRequirements: null,
    limitQuantityPerson: null,
    btnLoading: false,
    ticketId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('参数：', options)
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      shopName: wx.getStorageSync('shopName'),
      ticketId: options.ticketId
    })

    //  获取停车场
    this.getParkList(options.parkId)
    //  获取商户停车券
    this.getShopParkTicketList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 获取停车场
   */
  getParkList(parkId) {
    request.http(
      '/park/park-info/get-nearby-park-list', 'GET', {shopId: this.data.shopId, currentPage: 1, pageSize: 1000}
    ).then(res => {
      console.log('停车场信息：', res)
      let parkList = res.data.data.records
      let selectedParkList = parkList.filter( item => item.id == parkId )  
      this.setData({ parkList: parkList, selectedParkList: selectedParkList })
    })
  },
    /**
   * 获取商户停车券
   */
  getShopParkTicketList() {
    request.http(
      '/shop/shop-park-ticket/get-by-shop-id', 'GET', {shopId: this.data.shopId}
    ).then(res => {
      console.log('商户停车券信息：', res)
      let shopParkTicketList = res.data.data
      shopParkTicketList.forEach((shopParkTicket)=> {
        shopParkTicket.ticketName = shopParkTicket.parkingDuration + '小时停车券'
        if(shopParkTicket.id == this.data.ticketId){
          this.setData({ selectedParkTicket: shopParkTicket })
        }
      })
      this.setData({
        shopParkTicketList: shopParkTicketList
      })
    })
  },
  /**
   * 选择停车场
   */
  selectPark: function(e) {
    let selectedPark = this.data.parkList[e.detail.value]
    let isExists = false
    this.data.selectedParkList.forEach((item)=> {
      if(item.id == selectedPark.id){
        wx.showToast({ title: '【' + selectedPark.parkName + '】已存在', icon: 'none' })
        isExists = true
        return
      }
    })
    //  停车场已存在
    if(isExists){ return }

    //  选择停车场
    let selectedParkList = this.data.selectedParkList
    selectedParkList.push(selectedPark)
    this.setData({ selectedParkList: selectedParkList })
  },
  /**
   * 选择停车券
   */
  selectParkTicket(e){
    let selectedParkTicket = this.data.shopParkTicketList[e.detail.value]
    this.setData({ selectedParkTicket: selectedParkTicket })
  },
  /**
   * 删除停车场
   */
  deletePark(e){
    let index = e.target.dataset.index
    let selectedParkList = this.data.selectedParkList
    selectedParkList.splice(index, 1)
    this.setData({
      selectedParkList: selectedParkList
    })
  },
  /**
   * 获取配置数量
   */
  getSetQuantity(e){
    this.setData({  quantity: e.detail.value  })
    let quantity = e.detail.value
    if (quantity < 0 || quantity > 1000) {
      wx.showToast({ title: '请输入0到1000内的数字', icon: 'none' })
      this.setData({  quantity: null  })
      return
    }
  },
  /**
   * 每人限领数量
   */
  getLimitQuantityPerson(e){
    this.setData({ limitQuantityPerson: e.detail.value })
    let limitQuantityPerson = e.detail.value
    if (limitQuantityPerson < 0 || limitQuantityPerson > 1000) {
      wx.showToast({ title: '请输入0到1000内的数字', icon: 'none' })
      this.setData({ limitQuantityPerson: null })
      return
    }
  },
  /**
   * 获取待激活天数
   */
  getValidityDays(e){
    this.setData({ toBeActivatedValidityDays: e.detail.value })
    let validityDays = e.detail.value
    if (validityDays <= 0 || validityDays > 365) {
      wx.showToast({ title: '请输入0到365内的数字', icon: 'none' })
      this.setData({ toBeActivatedValidityDays: null })
      return
    }
  },
  /**
   * 使用条件
   */
  getActivationRequirements(e){
    let activationRequirements = e.detail.value
    this.setData({
      activationRequirements: activationRequirements
    })
  },
  /**
   * 提交
   */
  submit(){
    if(this.data.selectedParkList.length == 0){
      wx.showToast({ title: '请选择停车场', icon: 'none'  })
      return
    }
    if(this.data.activationRequirements == null || this.data.activationRequirements == ''){
      wx.showToast({ title: '使用条件不能为空', icon: 'none'  })
      return
    }
    if(this.data.limitQuantityPerson == null || this.data.limitQuantityPerson == ''){
      wx.showToast({ title: '每人限领数量不能为空', icon: 'none'  })
      return
    }
    if(this.data.quantity == null || this.data.quantity == ''){
      wx.showToast({ title: '张数不能为空', icon: 'none'  })
      return
    }
    if(this.data.toBeActivatedValidityDays == null || this.data.toBeActivatedValidityDays == ''){
      wx.showToast({ title: '待激活天数不能为空', icon: 'none' })
      return
    }

    let parkIds = this.data.selectedParkList.map(item => item.id)
    let params = {
      activationRequirements: this.data.activationRequirements,
      limitQuantityPerson: this.data.limitQuantityPerson,
      parkIds: parkIds,
      quantity: this.data.quantity,
      shopParkTicketId: this.data.selectedParkTicket.id,
      toBeActivatedValidityDays: this.data.toBeActivatedValidityDays
    }
    this.setData({ btnLoading: true })
    request.http(
      '/shop/park-ticket-set/add', 'POST', params, 0
    ).then(res => {
      this.setData({ btnLoading: false })
      if (res.data.code == 200) {
        wx.showToast({ title: '配置成功' })
        wx.redirectTo({  url: '/pages/mine/mine?activityTab=2' })
      }
    })
  },
  /**
   * 返回
   */
  back: function() {
    wx.navigateBack({ delta: 1 })
  },

})